var gulp = require('gulp');
var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();
var useref = require('gulp-useref');
var uglify = require('gulp-uglify');
var gulpIf = require('gulp-if');
var cssnano = require('gulp-cssnano');
var imagemin = require('gulp-imagemin');
var cache = require('gulp-cache');
var rename = require('gulp-rename');
var del = require('del');
var runSequence = require('run-sequence');

var gutil = require('gulp-util');
var bower = require('bower');
var concat = require('gulp-concat');
var minifyCss = require('gulp-minify-css');
var sh = require('shelljs');
var ngmin = require('gulp-ngmin');
var stripDebug = require('gulp-strip-debug');

var webserver = require('gulp-webserver');

gulp.task('webserver', function () {
    gulp.src('./')
      .pipe(webserver({
        host: 'localhost',
        port: 3000,
        livereload: true,
        open: './host/',
        directoryListing: {
          enable: true,
          path: './app'
        },
        proxies: [
          {
              source: '/api', target: 'http://39.108.133.65/'
          },
          {
            source: '/host', target: 'http://localhost:8020/'
        }
        ]
      }))
  });
  

gulp.task('browserSync',function(){
     browserSync.init({
        port:8020,
        server: {
            baseDir:'./app',
            proxy:'douxiaoli.com'
        },
     });

    gulp.watch('./app/**/*.html').on('change',browserSync.reload);
    gulp.watch('./app/css/*.css').on('change',browserSync.reload);
    gulp.watch('./app/js/**/*.js').on('change',browserSync.reload);

    // Sass编译完成后再刷新
    gulp.watch('./app/sass/*.scss',['sass']);
});

// 压缩js
gulp.task('minify',
function() {
    return gulp.src('./app/js/**/*.js').pipe(ngmin({
        dynamic: false
    }))
    .pipe(stripDebug()).pipe(uglify({
        outSourceMap: false
    }))
    .pipe(gulp.dest('./dist/js/'));
});

// 编译sass
gulp.task('sass',function(){
    return gulp.src('./app/sass/**/*.scss')
    .pipe(sass({outputStyle:'compact'}))
    .pipe(gulp.dest('./app/css'))
    .pipe(browserSync.reload({
        stream: true
    }));
});


gulp.task('useref',function(){
    return gulp.src('./app/*.html')
        .pipe(useref())
        .pipe(gulp.dest('./dist'));
});

gulp.task('userefTpl',function(){
    return gulp.src('./app/tpl/**/*.html')
        .pipe(useref())
        .pipe(gulp.dest('./dist/tpl'));
});

gulp.task('images', function(){
  return gulp.src('./app/img/**/*.+(png|jpg|jpeg|gif|svg|ico)')
  .pipe(gulp.dest('./dist/img'));
});

gulp.task('fonts',function(){
    return gulp.src('./app/fonts/**/*')
    .pipe(gulp.dest('./dist/fonts'));
});

gulp.task('vendor',function(){
    return gulp.src('./app/vendor/**/*')
    .pipe(gulp.dest('./dist/vendor/'));
});

gulp.task('addImages',function(){
    return gulp.src('./app/images/**/*')
    .pipe(gulp.dest('./dist/images/'));
});

gulp.task('cssMinify',function(){
    return gulp.src("./app/css/*.css")
    .pipe(gulpIf('*.css',cssnano()))
    .pipe(gulp.dest("./dist/css"))
})

gulp.task('clean:dist',function(){
    return del.sync('./dist');
});

gulp.task('cache:clear',function(callback){
    return cache.clearAll(callback);
});


gulp.task('default',function(callback){
    runSequence(['sass','browserSync','webserver'],
        callback
    );
});

gulp.task('build',function(callback){
    runSequence('clean:dist',
        ['sass','useref','fonts','minify','cssMinify','userefTpl','images','vendor',"addImages"],
        callback
    );
});